<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/600753 (zh-CN, DDL); Windows/10.0.0 (Win64);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="886"/>

<div>
<span><div><br/></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">C++三大特性：封装、继承、多态，今天给大家好好说说继承的奥妙</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">1、虚函数：</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">　　</span><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">C++的虚函数主要作用是“运行时多态”，父类中提供虚函数的实现，为子类提供默认的函数实现。子类可以重写父类的虚函数实现子类的特殊化</span><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">2、纯虚函数：</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">　　C++中包含纯虚函数的类，被称为是“抽象类”。抽象类不能使用new出对象，只有实现了这个纯虚函数的子类才能new出对象。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">　　C++中的纯虚函数更像是“只提供申明，没有实现”，是对子类的约束，是“接口继承”。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">　　C++中的纯虚函数也是一种“运行时多态”。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;">3、普通函数：</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">　　普通函数是静态编译的，没有运行时多态，只会根据指针或引用的“字面值”类对象，调用自己的普通函数。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">　　普通函数是父类为子类提供的“强制实现”。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(51, 51, 51); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal;">　　因此，在继承关系中，子类不应该重写父类的普通函数，因为函数的调用至于类对象的字面值有关。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div style="margin: 10px auto; text-indent: 0px; font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 12px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;">1.纯虚函数</span></div><div style="margin: 10px auto; text-indent: 0px; font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 12px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;">virtual ReturnType Function()= 0;</span></div><div style="font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;">纯虚函数可以让类先具有一个操作名称，而没有操作内容，让派生类在继承时再去具体地给出定义。凡是含有纯虚函数的类叫做</span><a href="https://baike.baidu.com/item/%E6%8A%BD%E8%B1%A1%E7%B1%BB" style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal; color: rgb(111, 188, 76);">抽象类</a><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;">。这种类不能声明对象，只是作为基类为派生类服务。</span><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(255, 0, 255); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.5;">除非在派生类中完全实现基类中所有的的纯虚函数，否则，派生类也变成了抽象类，不能实例化对象。</span></div><div style="font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;">一般而言纯虚函数的函数体是缺省的，但是也可以给出纯虚函数的函数体（此时纯虚函数变为虚函数），这一点经常被人们忽视，调用纯虚函数的方法为baseclass::virtual function.</span></div><div style="font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;"> </span></div><div style="margin: 10px auto; text-indent: 0px; font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 12px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal;">2.纯虚函数存在的意义</span></div><div style="font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><div><span style="color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-size: 12px; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.5;">【答案1】四个字：制定标准</span></div><div><span style="color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-size: 12px; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.5;">纯虚函数是为你的程序制定一种标准，即只要你继承了我，就必须按照我和标准来，实现我所有的方法，否则你也是虚拟的，和JAVA里的接口一样，都是制定标准，为了使程序更加通用化，可重用性提高，让所有实现它或继承自它的子类全部按同一标准来工作，你想想大家都按标准来对程序的益处是什么？</span></div></div><div style="font-size: 12px; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.5;">【答案2】在很多情况下，基类本身生成对象是不合情理的。例如，动物作为一个基类可以派生出老虎、孔雀等子类，但动物本身生成对象明显不合常理。</span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="background-color: rgb(255, 255, 255); font-size: 12px;"><span style="font-size: 12px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(75, 75, 75); font-family: verdana, Arial, helvetica, sans-seriff; font-variant-caps: normal; font-variant-ligatures: normal; background-color: rgb(255, 255, 255); line-height: 1.5;">【答案3】父类中存在纯虚函数，子类中重写（override）该函数，可实现多态。</span></span></div><div style="margin: 10px auto; padding: 0px; text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="text-indent: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br/></span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.15);-en-codeblock:true;"><div>#include &quot;pch.h&quot;</div><div>#include &lt;iostream&gt;</div><div>using namespace std;</div><div>class A</div><div>{</div><div>public:</div><div>       virtual void out1() = 0;  ///由子类实现</div><div>       virtual ~A() {};</div><div>       virtual void out2() ///默认实现</div><div>       {</div><div>              cout &lt;&lt; &quot;A(out2)&quot; &lt;&lt; endl;</div><div>       }</div><div>       void out3() ///强制实现</div><div>       {</div><div>              cout &lt;&lt; &quot;A(out3)&quot; &lt;&lt; endl;</div><div>       }</div><div>};</div><div>class B :public A</div><div>{</div><div>public:</div><div>       virtual ~B() {};</div><div>       void out1()</div><div>       {</div><div>              cout &lt;&lt; &quot;B(out1)&quot; &lt;&lt; endl;</div><div>       }</div><div>       void out2()</div><div>       {</div><div>              cout &lt;&lt; &quot;B(out2)&quot; &lt;&lt; endl;</div><div>       }</div><div>       void out3()</div><div>       {</div><div>              cout &lt;&lt; &quot;B(out3)&quot; &lt;&lt; endl;</div><div>       }</div><div>};</div><div>int main()</div><div>{</div><div>       A *ab = new B;</div><div>       ab-&gt;out1();</div><div>       ab-&gt;out2();</div><div>       ab-&gt;out3();</div><div>       cout &lt;&lt; &quot;************************&quot; &lt;&lt; endl;</div><div>       B *bb = new B;</div><div>       bb-&gt;out1();</div><div>       bb-&gt;out2();</div><div>       bb-&gt;out3();</div><div>       delete ab;</div><div>       delete bb;</div><div>       return 0;</div><div>}</div></div><div><br/></div><div><br/></div><div>虚函数是默认实现，如果子类有那么就实现子类的，如果没有就实现基类的</div><div><br/></div><div><br/></div></span>
</div></body></html> 